NASM BITS ALIGN 用法小结 您所在的位置:网站首页 NASM指令ALIGN 32<转自httpwwwcqlrjdcnnewscenter NASM BITS ALIGN 用法小结

NASM BITS ALIGN 用法小结

2024-06-28 14:55| 来源: 网络整理| 查看: 265

一、NASM文档中对BITS和ALIGN的介绍

(1) 6.1 BITS: Specifying Target Processor Mode

      The BITS directive specifies whether NASM should generate code designed to run on a processor operatingin 16−bit mode, 32−bit mode or 64−bit mode. The syntax is BITS XX, where XX is 16, 32 or 64.

      In most cases, you should not need to use BITS explicitly. The aout, coff, elf, macho, win32 andwin64 object formats, which are designed for use in 32−bit or 64−bit operating systems, all cause NASM toselect 32−bit or 64−bit mode, respectively, by default. The obj object format allows you to specify eachsegment you define as either USE16 or USE32, and NASM will set its operating mode accordingly, so theuse of the BITS directive is once again unnecessary.

       The most likely reason for using the BITS directive is to write 32−bit or 64−bit code in a flat binary file; this is because the bin output format defaults to 16−bit mode in anticipation of it being used most frequently to write DOS .COM programs, DOS .SYS device drivers and boot loader software.        You do not need to specify BITS 32 merely in order to use 32−bit instructions in a 16−bit DOS program; if you do, the assembler will generate incorrect code because it will be writing code targeted at a 32−bit platform, to be run on a 16−bit one.         When NASM is in BITS 16 mode, instructions which use 32−bit data are prefixed with an 0x66 byte, and thosereferring to 32−bit addresses have an 0x67 prefix. In BITS 32 mode, the reverse is true: 32−bit instructions require no prefixes, whereas instructions using 16−bit data need an 0x66 and those working on 16−bit addresses need an 0x67.       When NASM is in BITS 64 mode, most instructions operate the same as they do for BITS 32 mode. However, there are 8 more general and SSE registers, and 16−bit addressing is no longer supported.       The default address size is 64 bits; 32−bit addressing can be selected with the 0x67 prefix. The default operand size is still 32 bits, however, and the 0x66 prefix selects 16−bit operand size. The REX prefix is used both to select 64−bit operand size, and to access the new registers. NASM automatically inserts REX prefixes when necessary.

      When the REX prefix is used, the processor does not know how to address the AH, BH, CH or DH (high 8−bitlegacy) registers. Instead, it is possible to access the the low 8−bits of the SP, BP SI and DI registers as SPL,BPL, SIL and DIL, respectively; but only when the REX prefix is used.

       The BITS directive has an exactly equivalent primitive form, [BITS 16], [BITS 32] and [BITS 64].The user−level form is a macro which has no function other than to call the primitive form.        Note that the space is neccessary, e.g. BITS32 will not work!

(2)ALIGN



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有